package servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GetValidationCodeServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("image/jpeg");
		
		char [] choiceCodes = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
				'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
				'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		BufferedImage bfimage = new BufferedImage(80,40,BufferedImage.TYPE_INT_RGB);
		Graphics2D gimage = bfimage.createGraphics();
		gimage.setBackground(Color.yellow);
		gimage.setColor(Color.gray);
//		gimage.fillRect(0, 0, 100, 50);
		gimage.drawRect(2, 2, 75, 35);
		
		Font font = new Font("Fixedsys", Font.PLAIN, 22);
//		设置字体。
		gimage.setFont(font); 
		
		Random r = new Random();
		StringBuffer vcode = new StringBuffer();
		for(int i=0;i<4;i++){
			String randcode = String.valueOf(choiceCodes[r.nextInt(choiceCodes.length)]);
			vcode.append(randcode);
			gimage.drawString(randcode, 20*i+r.nextInt(5), 25);
		}
		HttpSession session = request.getSession();
		session.setAttribute("validationCode", vcode.toString());
		
		System.out.println(vcode);
		
		
		
		ServletOutputStream sos = response.getOutputStream();
		ImageIO.write(bfimage, "jpeg", sos);
		sos.close();
		
		
//		PrintWriter out = response.getWriter();
//		out
//				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//		out.println("<HTML>");
//		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//		out.println("  <BODY>");
//		out.print("    This is ");
//		out.print(this.getClass());
//		out.println(", using the POST method");
//		out.println("  </BODY>");
//		out.println("</HTML>");
//		out.flush();
//		out.close();
	}

}
